package org.jeecg.modules.clockRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.clockRecord.entity.ClockRecord;
import org.jeecg.modules.clockRecord.mapper.ClockRecordMapper;
import org.jeecg.modules.clockRecord.service.IClockRecordService;
import org.jeecg.modules.scheduling.entity.Scheduling;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalTime;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 打卡记录
 * @Author: jeecg-boot
 * @Date: 2025-01-26
 * @Version: V1.0
 */
@Service
@Slf4j
public class ClockRecordServiceImpl extends ServiceImpl<ClockRecordMapper, ClockRecord> implements IClockRecordService {

    @Override
    public ClockRecord getYesterdayByAdmitGuid(String admitGuid) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String s = DateUtils.formatDate(calendar, "yyyy-MM-dd");
        QueryWrapper<ClockRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("DATE(date)", s);
        wrapper.eq("admit_guid", admitGuid);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public ClockRecord getTodayByAdmitGuid(String admitGuid) {
        String s = DateUtils.formatDate("yyyy-MM-dd");
        QueryWrapper<ClockRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("DATE(date)", s);
        wrapper.eq("admit_guid", admitGuid);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public List<ClockRecord> getTodayAttendance() {
        if (hasNowExceededHour(17)){
            return baseMapper.getTodayAttendance();
        } else {
            return baseMapper.getTodayAttendance2();
        }
    }

    @Override
    public List<ClockRecord> getToday() {
        return baseMapper.getToday();
    }

    @Override
    public List<Map<String, Object>> getLeft1(String start, String end) {
        List<Map<String, Object>> left1 = baseMapper.getLeft1(start,end);
        return left1.stream().peek(m -> {
            switch ((int) m.get("name")) {
                case 1:
                    m.put("name", "白班");
                    break;
                case 2:
                    m.put("name", "夜班");
                    break;
                case 3:
                    m.put("name", "常白班");
                    break;
            }
        }).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getLeft2(String start, String end) {
        return baseMapper.getLeft2(start,end);
    }

    @Override
    public List<Map<String, Object>> getCenter3() {
        return baseMapper.getCenter3();
    }

    @Override
    public List<Map<String, Object>> getPersonInfo(String admitGuid, String monthStart,
                                                   String nextMonthStart) {
        return baseMapper.getPersonInfo(admitGuid, monthStart, nextMonthStart);
    }

    @Override
    public ClockRecord getLastByAdmitGuid(String admitGuid) {
        LambdaQueryWrapper<ClockRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ClockRecord::getAdmitGuid, admitGuid);
        wrapper.orderByDesc(ClockRecord::getCreateTime);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    public boolean hasNowExceededHour(int hour){
        // 获取当前系统时间
        LocalTime currentTime = LocalTime.now();

        // 定义中午12点的时间
        LocalTime noonTime = LocalTime.of(hour, 0);

        // 判断当前时间是否大于12点
        //            log.info("当前时间大于{}点: {}", hour, currentTime);
        //            log.info("当前时间小于或等于{}点: {}", hour, currentTime);
        return currentTime.isAfter(noonTime);
    }

}
